<?php

class Core_Block
{
	static $_instance = null;

	public static function getConfigs()
	{
		return new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
	}

	public static function getInstance()
	{
			if (null === self::$_instance) {
					self::$_instance = new self();
			}
			return self::$_instance;
	}

	public static function getBlock($path)
	{
		$basePath = APPLICATION_PATH . '/blocks/'.$path;
		$file 		= APPLICATION_PATH . '/blocks/'.$path.'/'.@end(explode('/',$path)).'.php';
		$class 		= 'Blocks_'.str_replace('/', '_', $path);
		if(is_file($file))
		{
			require_once($file);
			if(class_exists($class))
			{
				return new $class(self::getConfigs()->resources->view->toArray()+array('basePath'=>$basePath));
			}
		}
		return null;
	}
	
	public static function getRender($path, $options = array(), $name = 'render.phtml')
	{
		$begin		= microtime(true);
		$basePath = APPLICATION_PATH . '/blocks/'.$path;
		$file 		= APPLICATION_PATH . '/blocks/'.$path.'/'.@array_pop(explode('/',$path)).'.php';
		$class 		= 'Blocks_'.str_replace('/', '_', $path);
		if(is_file($file))
		{
			require_once($file);
			if(class_exists($class))
			{
				$ins = new $class(self::getConfigs()->resources->view->toArray()+array('basePath'=>$basePath));
				if(is_array($options))
				foreach($options as $k=>$v)
				{
					$ins->{$k} = $v;
				}
				$rs = $ins->render($name);
				return isset($_GET['time'])?('<div style="background:#f00;color:#fff;">'.( microtime(true)-$begin).'</div>'.$rs):$rs;
			}
		}
		return null;
	}
}

class Core_Block_Abstract extends Zend_View
{
	public function __construct($config)
	{
		parent::__construct($config);
	}
	public function render($name = 'render.phtml')
	{
		return parent::render($name);
	}
}